﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace BrokerBase.Lib.Policy
{
    public class ProductDuty
    {
        /// <summary>
        /// 身故保险金
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc1(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("身故"))
            {
                double f = ppa.GetDutyTotalValue("养老保险金");

                da.Value = ppa.TotalFee - f;

                ppa.IsCurrentFinish = true;
            }
        }
        /// <summary>
        /// 重度失能
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc2(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("重度失能"))
            {
                if (ppa.Period==0)
                {
                    da.Value = ppa.TotalFee;
                    ppa.IsCurrentFinish = true;
                }
                else
                {

                    da.Value = ppa.Amount * 0.01 * 12;
                    ppa.Fee = 0;

                }
            }
            else if (!ppa.SchemePeriodActing.InsurantStates.Contains("重度失能") && ppa.IsAtPaymentPeriod)
            {
                ppa.Fee = ppa.PolicyActing.Policy.Fee;
            }
        }
        /// <summary>
        /// 一般失能
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc3(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("全残"))
            {
                if (ppa.Period == 0)
                {
                    da.Value = ppa.TotalFee;
                    
                }
                else
                {
                    double f1 = ppa.GetDutyTotalValue("重度失能");
                    double f2 = ppa.GetDutyTotalValue("一般失能");
                    double f3 = ppa.GetDutyTotalValue("公共交通意外一般失能");

                    da.Value = ppa.Amount - f1 - f2 - f3;
                }
                ppa.IsCurrentFinish = true;
            }
        }

        /// <summary>
        /// 期满保险金
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc4(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.IsAtPaymentPeriod)
            {
                ppa.Amount = ppa.PolicyActing.Policy.Amount * 1.025 * ppa.Period;
            }
        }
        /// <summary>
        /// 分红
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc200(IPolicyPeriodActing ppa, IDutyActing da)
        {
            double f = (double)ppa.SchemePeriodActing.SchemeActing.GetAuxConfigValue("分红率(预估)");

            da.Value = ppa.TotalFee * f;

        }

        public void Calc5(IPolicyPeriodActing ppa, IDutyActing da)
        {
            string y = ppa.PolicyActing.Policy.SelectedColRateFactors.FirstOrDefault(p => p.Key == "养老金领取年限").Value;

            int m = 0;

            if (y == "30岁")
            {
                m = 30;
            }
            else if (y == "45岁")
            {
                m = 45;
            }
            else if (y == "50岁")
            {
                m = 50;
            }
            else if (y == "55岁")
            {
                m = 55;
            }
            else if (y == "60岁")
            {
                m = 60;
            }
            else if (y == "65岁")
            {
                m = 65;
            }
            else if (y == "即期领取")
            {
                m = ppa.Age;
            }

            double f = (double)ppa.SchemePeriodActing.SchemeActing.GetAuxConfigValue("分红率(预估)");

            if (ppa.Age < m)
            {
                double v=ppa.TotalFee * f;
                da.Tag=v;
                ppa.Amount += v;
                
            }
            else if (ppa.Age == m)
            {
                double s = 0;

                for (int i = 0; i < ppa.Period; i++)
                {
                    IDutyActing da2 = ppa.DutyActings.FirstOrDefault(p => p.Duty.Name == "分红");
                    s += da2.Tag == null ? 0 : (double)da2.Tag;
                }

                da.Value = s;

                
            }
            else if (ppa.SchemePeriodActing.InsurantStates.Contains("身故") && ppa.Age<m)
            {
                double s = 0;

                for (int i = 0; i < ppa.Period; i++)
                {
                    IDutyActing da2 = ppa.DutyActings.FirstOrDefault(p => p.Duty.Name == "分红");
                    s += da2.Tag == null ? 0 : (double)da2.Tag;
                }

                da.Value = s;

                da.Description = "身故支付累计红利";
            }

            

        }

        /// <summary>
        /// 豁免保费
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc6(IPolicyPeriodActing ppa, IDutyActing da)
        {


            if (ppa.Period > 0 &&  ppa.PolicyActing.PeriodActings[ppa.Period - 1].SchemePeriodActing.HolderStates.Contains("全残"))
            {
                foreach (IPolicyPeriodActing ppa2 in ppa.SchemePeriodActing.PolicyPeriodActings)
                {
                    ppa2.Fee = 0;
                }

                ppa.IsCurrentFinish = true;
            }


        }

        public void Calc7(IPolicyPeriodActing ppa, IDutyActing da)
        {

            if (ppa.SchemePeriodActing.InsurantStates.Contains("身故"))
            {
                if (ppa.Period == 0)
                {
                    da.Value = ppa.TotalFee;
                    ppa.IsCurrentFinish = true;
                    da.Description = "无息返还已交保费";
                }
                else if (ppa.Age < 18)
                {
                    double s = 0;

                    for (int i = 0; i <= ppa.Period; i++)
                    {
                        s += ppa.PolicyActing.PeriodActings[i].Fee * Math.Pow(1.03, i);
                    }

                    da.Value = s;
                    ppa.IsCurrentFinish = true;
                    da.Description = "已缴每期保险费本息和的加总";
                }
                else if (ppa.Age > 18 && ppa.Age <= 60)
                {
                    da.Value = ppa.Amount * 3;
                    ppa.IsCurrentFinish = true;
                    da.Description = "按三倍的基本保险金额给付身故保险金";
                }
                else if (ppa.Age > 60)
                {
                    da.Value = ppa.Amount;
                    ppa.IsCurrentFinish = true;
                    da.Description = "按基本保险金额给付身故保险金";

                }
            }
        }

        public void Calc8(IPolicyPeriodActing ppa, IDutyActing da)
        {

            if (ppa.SchemePeriodActing.InsurantStates.Contains("烧烫伤"))
            {
                string s = ppa.PolicyActing.SchemeActing.GetAuxConfigValue("烧烫伤部位").ToString();

                if (s == "头部等于3%")
                {
                    da.Value = ppa.Amount * 0.4;

                }
                else if (s == "头部等于3%并引致视力或眼球受损")
                {
                    da.Value = ppa.Amount * 0.6;
                }
                else if (s == "头部＞3%且＜8%")
                {
                    da.Value = ppa.Amount * 0.75;
                }
                else if (s == "头部＞3%且＜8%并引致视力或眼球受损")
                {
                    da.Value = ppa.Amount ;
                }
                else if (s == "头部≥8%")
                {
                    da.Value = ppa.Amount;
                }
                else if (s == "肢体＞10%且＜20%")
                {
                    da.Value = ppa.Amount * 0.5;
                }
                else if (s == "肢体＞10%且＜20%并累及手掌Ⅲ度烧伤")
                {
                    da.Value = ppa.Amount * 0.75;
                }
                else if (s == "肢体≥20%")
                {
                    da.Value = ppa.Amount ;
                }
            }

        }
        /// <summary>
        /// 豁免保费
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc9(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.Age == 100)
            {
                da.Value = ppa.Amount;
                ppa.IsCurrentFinish = true;
                
            }

        }
        /// <summary>
        /// 生存保险金
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc10(IPolicyPeriodActing ppa, IDutyActing da)
        {

            string y = ppa.PolicyActing.Policy.SelectedColRateFactors.FirstOrDefault(p => p.Key == "养老金领取年限").Value;

            int m = 0;

            if (y == "30岁")
            {
                m = 30;
            }
            else if (y == "45岁")
            {
                m = 45;
            }
            else if (y == "50岁")
            {
                m = 50;
            }
            else if (y == "55岁")
            {
                m = 55;
            }
            else if (y == "60岁")
            {
                m = 60;
            }
            else if (y == "65岁")
            {
                m = 65;
            }
            else if (y == "即期领取")
            {
                m = ppa.Age;
            }

            if (ppa.Age >= m)
            {
                da.Value = ppa.Amount * 0.09;
            }

        }
        /// <summary>
        /// 身故，全残
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc11(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("身故"))
            {
                da.Value = ppa.Amount;
                da.Description = "如果在180内身故，给付已交保费和保额10%之和";

            }

            if (ppa.Period >= 3  )
            {
                IDutyActing d = ppa.PolicyActing.PeriodActings[ppa.Period - 3].DutyActings.First(p => p.Duty.Name == "癌症保险金");

                double f = ppa.PolicyActing.PeriodActings.Where(p => p.Period < ppa.Period).Sum(q => q.DutyActings.First(k => k.Duty.Name == "长期检查保险金").Value);

                if (d.Tag != null && (bool)d.Tag && f<double.Epsilon)
                {
                    da.Value = ppa.Amount * 0.5;
                }
            }

        }
        /// <summary>
        /// 保额递增
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc12(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.Period>0 && ppa.Age<=64)
            {
                ppa.Amount = ppa.Amount * 0.04;

            }else if(ppa.Age>=65 && ppa.Age <=84)
            {
                ppa.Amount = ppa.Amount * 0.2;
            }

        }
        /// <summary>
        /// 保费打折
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc13(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.IsAtPaymentPeriod && ppa.Period>3)
            {
                double v = ppa.PolicyActing.PeriodActings.Where(p => p.Period >= ppa.Period - 3 && p.Period < ppa.Period).Sum(q => q.TotalValue);

                if (v < double.Epsilon)
                {
                    ppa.Fee = ppa.PolicyActing.Policy.Fee * 0.9;
                }
            }

        }
        /// <summary>
        /// 住院
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc14(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("住院") || ppa.SchemePeriodActing.InsurantStates.Contains("重症监护室"))
            {
                da.Value = ppa.Amount * 0.002;
            }

        }

        public void Calc15(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("身故") )
            {

                da.Value = ppa.TotalFee;
                ppa.IsCurrentFinish = true;
            }

        }
        /// <summary>
        /// 意外身故保险金给付
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc300(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("意外身故"))
            {
                double s = ppa.GetDutyTotalValue("意外残疾");

                da.Value = ppa.Amount - s;

                ppa.IsCurrentFinish = true;
            }
        }
        /// <summary>
        /// 意外身故保险金给付
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc301(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("意外残疾"))
            {

                string level = ppa.SchemePeriodActing.SchemeActing.GetAuxConfigValue("残疾等级").ToString();

                double c = 1f;

                if (level == "第一级")
                {
                    c = 1.0f;
                }
                else if (level == "第二级")
                {
                    c = 0.75f;
                }
                else if (level == "第三级")
                {
                    c = 0.5f;
                }
                else if (level == "第四级")
                {
                    c = 0.3f;
                }
                else if (level == "第五级")
                {
                    c = 0.2f;
                }
                else if (level == "第六级")
                {
                    c = 0.15;
                }
                else if (level == "第七级")
                {
                    c = 0.1f;
                }
                da.Value = ppa.Amount * c;

            }
        }

        /// <summary>
        /// 水陆交通意外额外保险金给付
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc302(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("水陆交通意外身故"))
            {
                ppa.SchemePeriodActing.InsurantStates.Add("意外身故");

                double s = ppa.GetDutyTotalValue("意外残疾");

                da.Value = 2*(ppa.Amount - s);

                ppa.IsCurrentFinish = true;
            }
        }
        /// <summary>
        /// 航空意外额外保险金给付
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc303(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("水陆交通意外身故"))
            {
                ppa.SchemePeriodActing.InsurantStates.Add("意外身故");

                double s = ppa.GetDutyTotalValue("意外残疾");

                da.Value = 4 * (ppa.Amount - s);

                ppa.IsCurrentFinish = true;
            }
        }
        /// <summary>
        /// 生命住院费用补偿医疗保险（B款）（C款）
        /// 住院费用补偿医疗保险金给付
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc304(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("住院"))
            {
                da.Value = ppa.Amount;
                da.Description = "最高限额";
            }
        }
        /// <summary>
        /// 生命住院费用补偿医疗保险（B款）（C款）
        /// 无理赔优惠
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc305(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.Period > 0 && ppa.PolicyActing.PeriodActings[ppa.Period - 1].DutyActings.FirstOrDefault(p => p.Duty.Name == "住院费用补偿医疗保险").Value <= double.Epsilon)
            {
                ppa.Amount = ppa.PolicyActing.Policy.Amount * 1.2;
            }
            else
            {
                ppa.Amount = ppa.PolicyActing.Policy.Amount;
            }
        }
        /// <summary>
        /// 生命住院每日补贴医疗保险
        /// 
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc306(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("住院"))
            {
                int d = (int)ppa.SchemePeriodActing.SchemeActing.GetAuxConfigValue("住院天数");

                da.Value = ppa.Amount;
                da.Description = "最高限额";
            }
        }
        /// <summary>
        /// 生命福鑫两全保险
        /// 身故保险金
        /// </summary>
        /// <param name="ppa"></param>
        /// <param name="da"></param>
        public void Calc307(IPolicyPeriodActing ppa, IDutyActing da)
        {
            if (ppa.SchemePeriodActing.InsurantStates.Contains("身故"))
            {
                IPolicy ep = ppa.SchemePeriodActing.SchemeActing.Scheme.Policies.FirstOrDefault(p => p.Product.Name == "生命_福康重大疾病保险_附_A款");

                double s = ppa.PolicyActing.Policy.Fee + (ep == null ? 0 : ep.Fee);

                int pl = ppa.IsAtPaymentPeriod ? ppa.Period + 1 : ppa.PolicyActing.Policy.PaymentPeriodLength;

                da.Value = pl * s;
            }
        }

    }
}
